package com.sirdc.modules.sys.web.index;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.Message;


import com.sirdc.modules.sys.entity.SysLogin;
import com.sirdc.modules.sys.filter.SysLoginFilter;
import com.sirdc.modules.sys.service.SysLoginService;
import com.sirdc.modules.sys.service.SysUserService;
import com.sirdc.modules.utils.ObjectUtils;
import net.huashitong.appsecurity.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.WebUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 网页版首页
 */
@Controller
@RequestMapping("/index")
public class IndexController  extends JsonBaseController {
    @Autowired
    private SysLoginService sysLoginService;
    @Autowired
    private SysUserService sysUserService;
    @Override
    protected String getView(String view) {
        return "/index/" + view;
    }

//    //数字峰会
//    @RequestMapping(value = "/", method = RequestMethod.GET)
//    public String goIndex(){
//        return getView("index");
//    }

    /**
     * GDP跳转页面
     * @return
     */
    @RequestMapping(value = "/gdp", method = RequestMethod.GET)
    public String goGdp(){
        return getView("/indicators/gdp");
    }
    /**
     * 第一产业跳转页面
     * @return
     */
    @RequestMapping(value = "/oneProduction", method = RequestMethod.GET)
    public String goOneProduction(){
        return getView("/indicators/oneProduction");
    }
    /**
     * 第二产业跳转页面
     * @return
     */
    @RequestMapping(value = "/twoProduction", method = RequestMethod.GET)
    public String goTwoProduction(){
        return getView("/indicators/twoProduction");
    }
    /**
     * 第三产业跳转页面
     * @return
     */
    @RequestMapping(value = "/threeProduction", method = RequestMethod.GET)
    public String goThreeProduction(){
        return getView("/indicators/threeProduction");
    }
    /**
     * 建筑业跳转页面
     * @return
     */
    @RequestMapping(value = "/buildingIndustry", method = RequestMethod.GET)
    public String goBuildingIndustry(){
        return getView("/indicators/buildingIndustry");
    }

    /**
     * 社会消费品零售总额跳转页面
     * @return
     */
    @RequestMapping(value = "/socialSpending", method = RequestMethod.GET)
    public String goSocialSpending(){
        return getView("/indicators/socialSpending");
    }

    /**
     * 规模以上工业跳转页面
     * @return
     */
    @RequestMapping(value = "/industrialScale", method = RequestMethod.GET)
    public String goIndustrialScale (){
        return getView("/indicators/industrialScale");
    }
    /**
     * 固定投資跳转页面
     * @return
     */
    @RequestMapping(value = "/fixedAssets", method = RequestMethod.GET)
    public String goFixedAssets(){
        return getView("/indicators/fixedAssets");
    }
    /**
     * 工业固定投資跳转页面
     * @return
     */
    @RequestMapping(value = "/industrialInvestment", method = RequestMethod.GET)
    public String goIndustrialInvestment(){
        return getView("/indicators/industrialInvestment");
    }
    /**
     * 公共预算总收入跳转页面
     * @return
     */
    @RequestMapping(value = "/budget1", method = RequestMethod.GET)
    public String goBudget1(){
        return getView("/indicators/budget-1");
    }
    /**
     * 公共预算收入跳转页面
     * @return
     */
    @RequestMapping(value = "/budget2", method = RequestMethod.GET)
    public String goBudget2(){
        return getView("/indicators/budget-2");
    }
    /**
     * 进出口总值跳转页面
     * @return
     */
    @RequestMapping(value = "/exports", method = RequestMethod.GET)
    public String goExports(){
        return getView("/indicators/exports");
    }
    /**
     * 实际外资跳转页面
     * @return
     */
    @RequestMapping(value = "/actualForeign", method = RequestMethod.GET)
    public String goActualForeign(){
        return getView("/indicators/actualForeign");
    }
    /**
     * 城镇人均收入跳转页面
     * @return
     */
    @RequestMapping(value = "/cityIncome", method = RequestMethod.GET)
    public String goCityIncome(){
        return getView("/indicators/cityIncome");
    }
    /**
     * 农村居民人均可支配收入跳转页面
     * @return
     */
    @RequestMapping(value = "/ruralIncome", method = RequestMethod.GET)
    public String goRuralIncome(){
        return getView("/indicators/ruralIncome");
    }
    /**
     * 财政主要考核指标跳转页面
     * @return
     */
    @RequestMapping(value = "/fiscalAssessment", method = RequestMethod.GET)
    public String goFiscalAssessment(){
        return getView("/indicators/fiscalAssessment");
    }
    /**
     * 税收收入完成情况跳转页面
     * @return
     */
    @RequestMapping(value = "/taxSituation", method = RequestMethod.GET)
    public String goTaxSituation(){
        return getView("/indicators/taxSituation");
    }

    /**
     * 获取预警数据列表
     * @return
     */
    @ResponseBody
    @RequestMapping("/queryIndicatorsList")
    public Message queryIndicatorsList(){
        List<Map<String, Object>> maps = new ArrayList<Map<String,Object>>();
        Map<String, Object> map1 = new HashMap<String, Object>();
        map1.put("name","地区生产总值");
        map1.put("url","/index/gdp");
        map1.put("industryId","A01");
        maps.add(map1);
        Map<String, Object> map2 = new HashMap<String, Object>();
        map2.put("name","第一产业增加值");
        map2.put("url","/index/oneProduction");
        map2.put("industryId","A02");
        maps.add(map2);
        Map<String, Object> map3 = new HashMap<String, Object>();
        map3.put("name","第二产业增加值");
        map3.put("url","/index/twoProduction");
        map3.put("industryId","A03");
        maps.add(map3);
        Map<String, Object> map4 = new HashMap<String, Object>();
        map4.put("name","第三产业增加值");
        map4.put("url","/index/threeProduction");
        map4.put("industryId","A06");
        maps.add(map4);
        Map<String, Object> map5 = new HashMap<String, Object>();
        map5.put("name","建筑业");
        map5.put("url","/index/buildingIndustry");
        map5.put("industryId","A04");
        maps.add(map5);
        Map<String, Object> map6 = new HashMap<String, Object>();
        map6.put("name","社会消费品零售总额");
        map6.put("url","/index/socialSpending");
        map6.put("industryId","A13");
        maps.add(map6);
        Map<String, Object> map7 = new HashMap<String, Object>();
        map7.put("name","规模以上工业");
        map7.put("url","/index/industrialScale");
        map7.put("industryId","A05");
        maps.add(map7);
        Map<String, Object> map8 = new HashMap<String, Object>();
        map8.put("name","固定资产投资");
        map8.put("url","/index/fixedAssets");
        map8.put("industryId","A07");
        maps.add(map8);
        Map<String, Object> map9 = new HashMap<String, Object>();
        map9.put("name","一般公共预算总收入");
        map9.put("url","/index/budget1");
        map9.put("industryId","A09");
        maps.add(map9);
        Map<String, Object> map10 = new HashMap<String, Object>();
        map10.put("name","一般公共预算收入");
        map10.put("url","/index/budget2");
        map10.put("industryId","A10");
        maps.add(map10);
        Map<String, Object> map11 = new HashMap<String, Object>();
        map11.put("name","进出口总值");
        map11.put("url","/index/exports");
        map11.put("industryId","A14");
        maps.add(map11);
        Map<String, Object> map12 = new HashMap<String, Object>();
        map12.put("name","工业固定资产投资");
        map12.put("url","/index/industrialInvestment");
        map12.put("industryId","A08");
        maps.add(map12);
        Map<String, Object> map13 = new HashMap<String, Object>();
        map13.put("name","实际利用外资");
        map13.put("url","/index/actualForeign");
        map13.put("industryId","A15");
        maps.add(map13);
        Map<String, Object> map14 = new HashMap<String, Object>();
        map14.put("name","城镇居民人均可支配收入");
        map14.put("url","/index/cityIncome");
        map14.put("industryId","A18");
        maps.add(map14);
        Map<String, Object> map15 = new HashMap<String, Object>();
        map15.put("name","农村居民人均可支配收入");
        map15.put("url","/index/ruralIncome");
        map15.put("industryId","A19");
        maps.add(map15);
        Map<String, Object> map16 = new HashMap<String, Object>();
        map16.put("name","财政主要考核指标");
        map16.put("url","/index/fiscalAssessment");
        map16.put("industryId","");
        maps.add(map16);
        Map<String, Object> map17 = new HashMap<String, Object>();
        map17.put("name","税收收入完成情况");
        map17.put("url","/index/taxSituation");
        map17.put("industryId","");
        maps.add(map17);
        return coverMessage("200", "获取成功",maps);
    }

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String goLogin(){
        return getView("/login");
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String goIndex2(Model model,HttpServletResponse response, HttpServletRequest request){
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        if(sysLoginService.isExistForUsername(username)){
           SysLogin syslogin=sysLoginService.getLoginByUsername(username);
           String encryptPass = sysUserService.getEncryPass(password, syslogin.getSaltKey());
           if(syslogin.getPassword().equals(encryptPass)){
               model.addAttribute("syslogin",syslogin);
               WebUtils.setSessionAttribute(request, "syslogin",syslogin);
               return getView("index");
           }else{
               model.addAttribute("mgs", "密码错误！");
               request.getSession().removeAttribute("syslogin");
           }
       }else{
            model.addAttribute("mgs", "用户不存！");
            request.getSession().removeAttribute("syslogin");
       }
       return getView("login");
    }

    /**
     * 退出登陆
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/exit", method = RequestMethod.GET)
    public String exit(HttpServletRequest request, HttpServletResponse response) {
        request.getSession().removeAttribute("syslogin");
        return getView("login");
    }


//    /**
//     * （数字峰会）退出登陆
//     * @param request
//     * @param response
//     * @return
//     */
//    @RequestMapping(value = "/exit", method = RequestMethod.GET)
//    public String exit(HttpServletRequest request, HttpServletResponse response) throws IOException {
//        request.getSession().removeAttribute("syslogin");
//        response.sendRedirect("http://120.78.158.120:8080");
//        return null;
//    }

    /**
     * 修改密码，验证原密码是否正确
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/check", method = RequestMethod.POST)
    public Object testIsUserAndPwd(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<String, Object>();
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        SysLogin syslogin=sysLoginService.getLoginByUsername(username);
        String encryptPass = sysUserService.getEncryPass(password, syslogin.getSaltKey());
        if(syslogin.getPassword().equals(encryptPass)){
            result.put("valid", true);
            return result;
        }else{
            result.put("valid", false);
            return result;
        }
    }
    /**
     * 修改用户密码
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/editPwd", method = RequestMethod.POST)
    public Message editUserPwd(@RequestBody SysLoginFilter sysLoginFilter) {
        String username = sysLoginFilter.getUsername();
        String password =sysLoginFilter.getPassword();
        String newPwd = sysLoginFilter.getNewPwd();
        String reNewPwd = sysLoginFilter.getReNewPwd();
        SysLogin syslogin=sysLoginService.getLoginByUsername(username);
        String encryptPass = sysUserService.getEncryPass(password, syslogin.getSaltKey());
        if(syslogin.getPassword().equals(encryptPass)){
            if (newPwd.equals(reNewPwd)) {
                //修改密码
                String encryptPasss = sysUserService.getEncryPass(newPwd, syslogin.getSaltKey());
                syslogin.setPassword(encryptPasss);
                sysLoginService.update(syslogin);
                return coverMessage("200", "密码修改成功", "1");
            } else {
                return coverMessage("200", "新密码两次输入不一致", "0");
            }
        }else{
            return coverMessage("200", "原密码不正确", "0");
        }
    }
}
